package com.ming.ksdstate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ming.ksdstate.entity.State;
import com.ming.ksdstate.entity.StateDetail;
import com.ming.ksdstate.entity.StateUserDetail;
import com.ming.ksdstate.service.detail.StateDetailService;
import com.ming.ksdstate.service.state.StateService;
import com.ming.ksdstate.service.stateuser.StateUserDetailService;
import com.ming.ksdstate.vo.R;
import com.ming.ksdstate.vo.StateDetailVo;
import com.ming.ksdstate.vo.StateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @description:
 * @author: xuke
 * @time: 2021/4/29 21:50
 */
@Controller
public class StateDetailController {

    @Autowired
    private StateService stateService;
    @Autowired
    private StateDetailService stateDetailService;
    @Autowired
    private StateUserDetailService stateUserDetailService;




//    /**
//     * @return
//     * @Author xuke
//     * @Description 需求：根据用户点击所在的统计中的选项，进行一个累加的动作
//     * @Date 22:43 2021/5/25
//     * @Param1 userid:?
//     * @Param1 stateid:?
//     * @Param1 stateitemid:?
//     **/
//    @PostMapping("/state/userdetail")
//    @ResponseBody
//    public R clickUserItem(Integer stateid, Integer detailid, HttpSession session) {
//        // 1: 获取当前登录的用户id
//        //Object user = session.getAttribute("user");
//        Integer userid = 1;
//        // 2: 根据统计id和统计项id和用户id查询是否已经投票过了.
//        LambdaQueryWrapper<StateUserDetail> queryWrapper = new LambdaQueryWrapper<StateUserDetail>();
//        queryWrapper.eq(StateUserDetail::getUserid, userid);
//        queryWrapper.eq(StateUserDetail::getStateid, stateid);
//        queryWrapper.eq(StateUserDetail::getDetailid, detailid);
//        // 3: 查询
//        StateUserDetail detailServiceOne = stateUserDetailService.getOne(queryWrapper);
//        if (detailServiceOne == null) {
//            // 4:对统计项进行一次累加
//            StateDetail stateDetail = stateDetailService.getById(detailid);
//            if (stateDetail != null) {
//                // 5：更新用户点击的统计项的数量
//                stateDetail.setItemnum(stateDetail.getItemnum() + 1);
//                stateDetailService.saveOrUpdate(stateDetail);
//
//                //6:保存用户统计项的明细
//                detailServiceOne = new StateUserDetail();
//                detailServiceOne.setDetailid(detailid);
//                detailServiceOne.setStateid(stateid);
//                detailServiceOne.setUserid(userid);
//                stateUserDetailService.saveOrUpdate(detailServiceOne);
//
//                return R.ok().message("投票成功!");
//            }
//            return R.error().message("你已投票过了!");
//        }
//
//        return R.error().message("投票失败!");
//    }


//    /**
//     * @return
//     * @Author xuke
//     * @Description 需求：根据用户点击所在的统计中的选项，进行一个累加的动作
//     * @Date 22:43 2021/5/25
//     * @Param1 userid:?
//     * @Param1 stateid:?
//     * @Param1 stateitemid:?
//     **/
//    @PostMapping("/state/userdetail")
//    @ResponseBody
//    public R clickUserItem(@RequestBody StateDetailVo stateDetailVo, HttpSession session) {
//        // 1: 获取当前登录的用户id
//        //Object user = session.getAttribute("user");
//        Integer userid = 1;
//        // 2: 根据统计id和统计项id和用户id查询是否已经投票过了.
//        LambdaQueryWrapper<StateUserDetail> queryWrapper = new LambdaQueryWrapper<StateUserDetail>();
//        queryWrapper.eq(StateUserDetail::getUserid, userid);
//        queryWrapper.eq(StateUserDetail::getStateid, stateDetailVo.getStateid());
//        queryWrapper.eq(StateUserDetail::getDetailid, stateDetailVo.getDetailid());
//        // 3: 查询
//        StateUserDetail detailServiceOne = stateUserDetailService.getOne(queryWrapper);
//        if (detailServiceOne == null) {
//            // 4:对统计项进行一次累加
//            StateDetail stateDetail = stateDetailService.getById(stateDetailVo.getDetailid());
//            if (stateDetail != null) {
//                // 5：更新用户点击的统计项的数量
//                stateDetail.setItemnum(stateDetail.getItemnum() + 1);
//                stateDetailService.saveOrUpdate(stateDetail);
//
//                //6:保存用户统计项的明细
//                detailServiceOne = new StateUserDetail();
//                detailServiceOne.setDetailid(stateDetailVo.getDetailid());
//                detailServiceOne.setStateid(stateDetailVo.getStateid());
//                detailServiceOne.setUserid(userid);
//                stateUserDetailService.saveOrUpdate(detailServiceOne);
//
//                return R.ok().message("投票成功!");
//            }
//            return R.error().message("你已投票过了!");
//        }
//
//        return R.error().message("投票失败!");
//    }


        /**
     * @return
     * @Author xuke
     * @Description 需求：根据用户点击所在的统计中的选项，进行一个累加的动作
     * @Date 22:43 2021/5/25
     * @Param1 userid:?
     * @Param1 stateid:?
     * @Param1 stateitemid:?
     **/
    @PostMapping("/state/userdetail")
    @ResponseBody
    public R clickUserItem(@RequestBody  StateDetailVo stateDetailVo, HttpSession session) {
        // 1: 获取当前登录的用户id
        //Object user = session.getAttribute("user");
        Integer userid = 1;//准备后台 准备登录
        // 2: 根据统计id和统计项id和用户id查询是否已经投票过了.
        LambdaQueryWrapper<StateUserDetail> queryWrapper = new LambdaQueryWrapper<StateUserDetail>();
        queryWrapper.eq(StateUserDetail::getUserid, userid);
        queryWrapper.eq(StateUserDetail::getStateid, stateDetailVo.getStateid());
        queryWrapper.eq(StateUserDetail::getDetailid, stateDetailVo.getDetailid());
        // 3: 查询
        StateUserDetail detailServiceOne = stateUserDetailService.getOne(queryWrapper);
        if (detailServiceOne == null) {
            // 4:对统计项进行一次累加
            StateDetail stateDetail = stateDetailService.getById(stateDetailVo.getDetailid());
            if (stateDetail != null) {
                // 5：更新用户点击的统计项的数量
                stateDetail.setItemnum(stateDetail.getItemnum() + 1);
                stateDetailService.saveOrUpdate(stateDetail);

                //6:保存用户统计项的明细
                detailServiceOne = new StateUserDetail();
                detailServiceOne.setDetailid(stateDetailVo.getDetailid());
                detailServiceOne.setStateid(stateDetailVo.getStateid());
                detailServiceOne.setUserid(userid);
                stateUserDetailService.saveOrUpdate(detailServiceOne);
                return R.ok().message("投票成功!");
            }
        }
        return R.error().message("你已投票过了!");

    }
}
